package com.bhkj.websocketlibrary.socketio;

import com.bhkj.websocketlibrary.ws.WsConfig;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;

import java.net.URISyntaxException;

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class SocketIoManager {
    ISocketIoCallback iSocketIoCallback;

    private Socket mSocket;

    private SocketIoManager() {
        init();
    }

    public static class Holder {
        private static SocketIoManager INSTANCE = new SocketIoManager();
    }

    public static SocketIoManager getInstance() {
        return Holder.INSTANCE;
    }

    private void init() {
        //创建socket客户端
        createSocketClient();
        //连接服务端
        if (mSocket != null) {
            mSocket.connect();
        }

        //注册登录回调事件
        registerLogin();
        //注册注销回调事件
        registerLogout();
        registerWindowStatus();
        registerOther();
    }

    private void createSocketClient() {
        LogUtils.d("创建Socket");
        String query = WsConfig.getInstance().getQuery();
        checkQuery(query);
        try {
            if (mSocket == null) {
                IO.Options options = new IO.Options();
                options.query = query;
                mSocket = IO.socket(WsConfig.getInstance().getUrl(), options);
            }
        } catch (URISyntaxException e) {
            e.printStackTrace();
            LogUtils.d("url解析错误:" + e.getReason());
        }
    }

    private void checkQuery(String query) {
        if (ObjectUtils.isEmpty(query)) {
            throw new RuntimeException("查询参数不能为空");
        }
//        if (!query.contains("http")) {
//            throw new RuntimeException("传入的参数不合法");
//        }
    }

    /**
     * 注册qlogin事件
     */
    private void registerLogin() {
        String loginType = WsConfig.getInstance().getLoginType();
        if (mSocket != null) {
            LogUtils.d(loginType);
            mSocket.on(loginType, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    LogUtils.d("loginType:" + args[0]);
                    //窗口屏接收登录消息
                    if (iSocketIoCallback != null) {
                        iSocketIoCallback.onLogin(String.valueOf(args[0]));
//                        LogUtils.d("loginType1:" + args[0]);
//                    if (ObjectUtils.equals(SocketEvent.QLOGIN, loginType)) {
//
//                    } else if (ObjectUtils.equals(SocketEvent.PLOGIN, loginType)) {
//                        //评价器接收登录消息
//                    } LogUtils.d("loginType:"+args[0]);
                    }
                }
            });
        }
    }

    /**
     * 注册登出监听事件
     */
    private void registerLogout() {
        String logoutType = WsConfig.getInstance().getLogoutType();
        if (mSocket != null) {
            mSocket.on(logoutType, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    if (iSocketIoCallback != null) {
                        iSocketIoCallback.onLogout(String.valueOf(args[0]));
                    }
                }
            });
        }
    }

    private void registerWindowStatus() {
        String windowStatusType = WsConfig.getInstance().getWindowStatusType();
        if (mSocket != null) {
            mSocket.on(windowStatusType, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    if (iSocketIoCallback != null) {
                        iSocketIoCallback.onWindowStatus(String.valueOf(args[0]));
                    }
                }
            });
        }
    }

    private void registerOther() {
        WsConfig instance = WsConfig.getInstance();
        String otherType = instance.getOtherType();

        if (mSocket != null && ObjectUtils.isNotEmpty(otherType)) {
            mSocket.on(otherType, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    if (iSocketIoCallback != null) {
                        iSocketIoCallback.onOther(String.valueOf(args[0]), otherType);
                    }
                }
            });
        }
    }


    public void toLogin(String loginId) {
        if (mSocket != null && mSocket.connected()) {
            mSocket.emit("login", loginId, "1");
        }
    }

    public void addCallback(ISocketIoCallback iSocketIoCallback) {
        this.iSocketIoCallback = iSocketIoCallback;
    }

}
